/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontend;


import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.apache.jasper.servlet.JspServlet;
import org.eclipse.jetty.server.Handler;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.server.handler.HandlerList;
import org.eclipse.jetty.server.handler.ResourceHandler;
import org.eclipse.jetty.servlet.DefaultServlet;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import servlet.WeatherServlet;
import servlet.listAllItemServlet;
import servlet.listAllTagServlet;
import servlet.listItemIDServlet;
import servlet.randomItemServlet;

/**
 *
 * @author chanhlt
 */
public class Frontend {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException, Exception {
        // TODO code application logic here
        
        Properties pro=new Properties();
        pro.load(new FileInputStream("src/conf/config.ini"));
        
        int port=Integer.parseInt(pro.getProperty("port_listen"));
        
        Server server=new Server(port);

        
        ServletContextHandler contextHandler=new ServletContextHandler(ServletContextHandler.SESSIONS);
        contextHandler.setContextPath("/");
        contextHandler.setResourceBase("src/webapp");
        contextHandler.setClassLoader(Thread.currentThread().getContextClassLoader());
        
        contextHandler.addServlet(DefaultServlet.class, "/");
        contextHandler.addServlet(new ServletHolder(new listAllItemServlet()), "/listAllItem");
        contextHandler.addServlet(new ServletHolder(new listItemIDServlet()), "/listItemID");
        contextHandler.addServlet(new ServletHolder(new randomItemServlet()), "/random");
        contextHandler.addServlet(new ServletHolder(new listAllTagServlet()), "/listTag");
        contextHandler.addServlet(JspServlet.class, "*.jsp");
        contextHandler.addServlet(new ServletHolder(new WeatherServlet()), "/WeatherServlet");
        
        
        ResourceHandler resource=new ResourceHandler();
        resource.setDirectoriesListed(true);
        resource.setWelcomeFiles(new String[] {"index.html"});
        resource.setResourceBase(".");
        
        HandlerList handlers=new HandlerList();
        handlers.setHandlers(new Handler[]{contextHandler, resource});
        
        server.setHandler(handlers);
        
        server.start();
        server.join();
    }
}
